package ch11.dataExchange;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class DataExchangeTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            var frame = new DataExchangeFrame();
            frame.setTitle("DataExchangeTest");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

class DataExchangeFrame extends JFrame {
    public static final int TEXT_ROWS = 20;
    public static final int TEXT_COLUMNS = 40;
    private PasswordChooser dialog = null;
    private JTextArea textArea;

    public DataExchangeFrame() {
       // 菜单
       var mbar = new JMenuBar();
       setJMenuBar(mbar);

       var fileMenu = new JMenu("File");
       mbar.add(fileMenu);

       var connectItem = new JMenuItem("Connect");
       connectItem.addActionListener(_ -> connect());
       fileMenu.add(connectItem);

       var exitItem = new JMenuItem("Exit");
       exitItem.addActionListener(_ -> System.exit(0));
       fileMenu.add(exitItem);

       // 文本区域
       textArea = new JTextArea(TEXT_ROWS, TEXT_COLUMNS);
       add(new JScrollPane(textArea), BorderLayout.CENTER);

       pack();
    }

    // Controller
    private void connect() {
        if (dialog == null) {
            dialog = new PasswordChooser();
        }

        // model -> view
        dialog.setUser(new User("codetimes", null));

        if (dialog.showDialog(this, "Connect")) {
            // view -> model
            var u = dialog.getUser();
            textArea.setText("user name = " + u.getName()
                + ", password = " + new String(u.getPassword()) + "\n");
        }
    }
}

// View
class PasswordChooser extends JPanel {
    private JTextField username;
    private JPasswordField password;
    private JButton okButton;
    private boolean ok;
    private JDialog dialog;

    public PasswordChooser() {
        setLayout(new BorderLayout());

        var panel = new JPanel();
        panel.setLayout(new GridLayout(2, 2));
        panel.add(new JLabel("User name:"));
        panel.add(username = new JTextField(""));
        panel.add(new JLabel("Password:"));
        panel.add(password = new JPasswordField(""));
        add(panel, BorderLayout.CENTER);

        okButton = new JButton("OK");
        okButton.addActionListener(_ -> {
            ok = true;
            dialog.setVisible(false);
        });

        var cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(_ -> dialog.setVisible(false));

        var buttonPanel = new JPanel();
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    public void setUser(User u) {
        username.setText(u.getName());
    }

    public User getUser() {
        return new User(username.getText(), password.getPassword());
    }

    public boolean showDialog(Component parent, String title) {
        ok = false;

        Frame owner = null;
        if (parent instanceof Frame p) {
            owner = p;
        }
        else {
            owner = (Frame)SwingUtilities.getAncestorOfClass(Frame.class, parent);
        }

        if (dialog == null || dialog.getOwner() != owner) {
            dialog = new JDialog(owner, title, true);
            dialog.add(this);   // 添加面板到对话框
            dialog.getRootPane().setDefaultButton(okButton);
            dialog.pack();
        }

        dialog.setLocationRelativeTo(owner);

        dialog.setTitle(title);
        dialog.setVisible(true);
        return ok;
    }
}

// Model
class User {
    private String name;
    private char[] password;

    public User(String aName, char[] aPassword) {
        name = aName;
        password = aPassword;
    }

    public String getName() {
        return name;
    }

    public char[] getPassword() {
        return password;
    }

    public void setName(String aName) {
        name = aName;
    }

    public void setPassword(char[] aPassword) {
        password = aPassword;
    }
}